package cac.fenjian.resource.partreceivesearch.service;

import cac.common.DateUtil;
import cac.fenjian.resource.WmsDeliverPlan.dto.WmsDeliverPlanDTO;
import cac.fenjian.resource.partreceivesearch.dao.PartReceiveDao;
import cac.gkinterface.service.WorkService;
import com.alibaba.fastjson.JSONObject;
import com.cac.dme.adapter.rest.msg.QueryReqBean;
import com.cac.dme.adapter.rest.msg.QueryRespBean;
import com.cac.dme.adapter.utils.ComUtil;
import com.cac.dme.adapter.utils.PageHelper;
import com.cac.dme.common.exception.service.DaoException;
import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.*;

@Service
@Slf4j
public class PartReceiveService {

    @Autowired
    private PartReceiveDao partReceiveDao;
    @Autowired
    private Environment environment;
    @Autowired
    private WorkService workService;

    //零件接收页面条件查询
    public QueryRespBean<WmsDeliverPlanDTO> searchParamsByBarcodeAM(QueryReqBean<WmsDeliverPlanDTO> queryReqBean, String orderBy, String Keywords, Date startTime, Date endTime,
                                                                    String itemCode) {
        QueryRespBean<WmsDeliverPlanDTO> queryRespBean = new QueryRespBean<>();
        try {
            PageHelper.startPage(queryReqBean.getPageParameter());
            WmsDeliverPlanDTO searchParams = queryReqBean.getSearchParams();
            Page<WmsDeliverPlanDTO> dataList = partReceiveDao.searchParamsByBarcodeAM(searchParams, orderBy, Keywords, startTime, endTime, itemCode);
            queryRespBean.setResult(dataList);
            return queryRespBean;
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }

    //条码查询
    public QueryRespBean<List<WmsDeliverPlanDTO>> searchParamsByBarcode(QueryReqBean<WmsDeliverPlanDTO> queryReqBean,
                                                                        String barcode,String shop) {
        QueryRespBean<List<WmsDeliverPlanDTO>> queryRespBean = new QueryRespBean<>();
        try {
            PageHelper.startPage(queryReqBean.getPageParameter());
            WmsDeliverPlanDTO searchParams = queryReqBean.getSearchParams();
            if(searchParams==null){
                searchParams = new WmsDeliverPlanDTO();
                searchParams.setRequireShop(shop);
            }else{
                searchParams.setRequireShop(shop);
            }
            Page<List<WmsDeliverPlanDTO>> dataList = partReceiveDao.searchByBarcodeAM(searchParams, barcode);
            queryRespBean.setResult(dataList);
            return queryRespBean;
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }

    public boolean queryPartHgzIsRepeat(String barcode,String shop){
        int count = partReceiveDao.queryPartHgzIsRepeat(barcode,shop);
        if(count>1){
            return true;
        }
        return false;
    }

    //查询是否发起协调
    public HashMap searchCoordinate(String barcode) {
        return partReceiveDao.searchCoordinate(barcode);
    }

    //根据前端按钮修改接收物料状态
    @Transactional
    public String changeMaterialSate(String barcode, String receiveEmpCode, String IP,String wmsDeliveryPlanId) throws ParseException {
        String result = null;
        try {
            //接收归零
            partReceiveDao.updateCoByBarcode(barcode, receiveEmpCode);
            //更改交接单状态
            int i = partReceiveDao.changeTransferType(barcode, DateUtil.nowTime(), receiveEmpCode, IP,wmsDeliveryPlanId);
            System.out.println("更新交接单：" + i);
            result = "OK";
        } catch (Exception e) {
            result = "IllegalStatus";
            e.printStackTrace();
            throw new DaoException(e.getMessage());
        } finally {
            return result;
        }
    }

    //批量接收
    public String changeMaterialSates(List<String> barcodes, String receiveEmpCode, String IP) throws ParseException {
        String result = "";
     /*   for (int i = 0; i < barcodes.size(); i++) {
            Integer exist = partReceiveDao.searchBarCode(barcodes.get(i));
            try {
                //条码是否存在
                if (exist != null) {
//                    partReceiveDao.updateCoByBarcode(barcodes.get(i));
                    partReceiveDao.changeTransferType(barcodes.get(i), DateUtil.nowTime(), receiveEmpCode, IP);
                    result = "OK";
                } else {
                    result = "IllegalBarcode";
                }
            } catch (Exception e) {
                throw new DaoException(e.getMessage());
            }
        }*/
        return result;
    }


    //归零接收
    @Transactional
    public String coordinateReceive(String barcode, String WMS_DELIVERY_COORDINATE_ID) {
        HashMap map = partReceiveDao.coordinateReceive(barcode);
        map.put("wmsCoordinateInfoId", WMS_DELIVERY_COORDINATE_ID);
        map.put("locatorNo", environment.getProperty("doReturnErp.scanZeroReceiveLj.locatorNo"));
        JSONObject data = new JSONObject(map);
        //放入请求头
        JSONObject headersMap = new JSONObject();
        headersMap.put("func", environment.getProperty("doReturnErp.scanZeroReceiveLj.func"));
        headersMap.put("data", data);
        String headersmap = headersMap.toJSONString();
        log.info("======================================================");
        //工控网上传数据
        HashMap issuedTest = new HashMap();
        issuedTest.put("BData", headersmap);
        issuedTest.put("type", environment.getProperty("doReturnErp.scanZeroReceiveLj.func"));
        log.info("工控网上传程序后台日志:>>>>>>>>>>>>>>>>>>>>>>>>>issuedParameters:" + issuedTest.toString());
        workService.issuedTest(issuedTest);
        //对已经转换成字符串的数据进行加密
        final Base64.Encoder encoder = Base64.getEncoder();
        String BData = encoder.encodeToString(headersmap.getBytes());
        return BData;
    }

    public List<HashMap> isBig(String barcode) {
        return partReceiveDao.isBig(barcode);
    }
}
